#include<bits/stdc++.h>
using namespace std;
const int N=42;

int n,m,q;
int edges[N][N];


int main(){
    scanf("%d%d",&n,&m);
    memset(edges,0x3f,sizeof(edges));
    for(int i=1;i<=n;i++){
        edges[i][i]=0;
    }
    for(int i=1;i<=m;i++){
        int s,t;
        scanf("%d%d",&s,&t);
        edges[s][t]=edges[t][s]=1;
    }
    for(int k=1;k<=n;k++){
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++){
                if(edges[i][k]+edges[k][j]<edges[i][j]){
                    edges[i][j]=edges[i][k]+edges[k][j];
                }
            }
        }
    }
    scanf("%d",&q);
    for(int i=1;i<=q;i++){
        int s,t;
        scanf("%d%d",&s,&t);
        for(int j=1;j<=n;j++){
            if(edges[s][t]==edges[s][j]+edges[j][t]) printf("%d ",j);
        }
        printf("\n");
    }
    return 0;
}